package com.qiyi.player.core.model.utils
{
   import flash.events.EventDispatcher;
   import com.qiyi.player.core.IDestroy;
   import flash.display.Loader;
   import flash.display.Bitmap;
   import com.qiyi.player.core.model.IMovie;
   import flash.events.Event;
   import flash.events.IOErrorEvent;
   import flash.events.SecurityErrorEvent;
   import flash.net.URLRequest;
   import flash.system.LoaderContext;
   import com.qiyi.player.core.model.impls.Segment;
   import com.qiyi.player.core.model.impls.Keyframe;
   import com.qiyi.player.core.Config;
   
   public class Capturer extends EventDispatcher implements IDestroy
   {
      
      public function Capturer() {
         super();
      }
      
      public static const Evt_Complete:String = "complete";
      
      private static const Date_Flag:int = 20101026;
      
      private var _loader:Loader;
      
      private var _bitmap:Bitmap;
      
      public function get bitmap() : Bitmap {
         return this._bitmap;
      }
      
      public function capture(param1:IMovie, param2:Number, param3:int) : Boolean {
         var _loc4_:String = null;
         this.reset();
         if(param1)
         {
            _loc4_ = this.getUrl(param1,param2,param3);
            if(_loc4_ != "")
            {
               this._loader = new Loader();
               this._loader.contentLoaderInfo.addEventListener(Event.COMPLETE,this.onCompleteHandler);
               this._loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,this.onErrorHandler);
               this._loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onErrorHandler);
               this._loader.load(new URLRequest(_loc4_),new LoaderContext(true));
               return true;
            }
         }
         return false;
      }
      
      public function getCaptureURL(param1:IMovie, param2:Number, param3:int) : String {
         return this.getUrl(param1,param2,param3);
      }
      
      public function reset() : void {
         this._bitmap = null;
         if(this._loader)
         {
            this._loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,this.onCompleteHandler);
            this._loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,this.onErrorHandler);
            this._loader.contentLoaderInfo.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onErrorHandler);
            this._loader = null;
         }
      }
      
      private function onCompleteHandler(param1:Event) : void {
         this._bitmap = this._loader.content as Bitmap;
         dispatchEvent(new Event(Evt_Complete));
      }
      
      private function onErrorHandler(param1:Event) : void {
         this._bitmap = null;
         dispatchEvent(new Event(Evt_Complete));
      }
      
      private function getUrl(param1:IMovie, param2:Number, param3:int) : String {
         var _loc5_:Segment = null;
         var _loc6_:Vector.<Keyframe> = null;
         var _loc7_:* = 0;
         var _loc8_:* = NaN;
         var _loc9_:* = NaN;
         var _loc10_:* = NaN;
         var _loc11_:Array = null;
         var _loc12_:* = 0;
         var _loc4_:* = "";
         if(param1)
         {
            _loc5_ = param1.getSegmentByTime(param2);
            if(_loc5_)
            {
               _loc6_ = _loc5_.getCaptureKeyFrames(param2);
               _loc7_ = _loc6_.length;
               if(_loc7_ > 0)
               {
                  _loc8_ = 0;
                  _loc9_ = 0;
                  _loc10_ = 0;
                  if(_loc7_ == 1)
                  {
                     _loc8_ = _loc6_[0].position;
                     _loc9_ = _loc5_.totalBytes;
                     _loc10_ = param2 - _loc6_[0].time;
                  }
                  else if(_loc7_ == 2)
                  {
                     _loc8_ = _loc6_[0].position;
                     _loc9_ = _loc6_[1].position;
                     _loc10_ = param2 - _loc6_[0].time;
                  }
                  
                  _loc4_ = _loc5_.url.replace(".f4v",".jpg");
                  _loc11_ = _loc4_.split("/");
                  _loc12_ = int(_loc11_[_loc11_.length - 2]);
                  _loc11_[2] = Config.CAPTURE_URL;
                  if(_loc12_ >= Date_Flag)
                  {
                     _loc11_[3] = "videos2";
                  }
                  else
                  {
                     _loc11_[3] = "videos";
                  }
                  _loc4_ = _loc11_.join("/") + "?";
                  _loc4_ = _loc4_ + ("start=" + _loc8_);
                  _loc4_ = _loc4_ + ("&end=" + _loc9_);
                  _loc4_ = _loc4_ + ("&time=" + _loc10_);
                  _loc4_ = _loc4_ + ("&mode=" + param3);
               }
            }
         }
         return _loc4_;
      }
      
      public function destroy() : void {
         this._bitmap = null;
         if(this._loader)
         {
            this._loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,this.onCompleteHandler);
            this._loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,this.onErrorHandler);
            this._loader.contentLoaderInfo.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onErrorHandler);
            this._loader = null;
         }
      }
   }
}
